/**
 * @Author Guy Franco
 *
 * 	this is theComplete Request Panel able to search relevant requests complete them and update the DB
 * 
 */

package gui;

import javax.swing.JPanel;

import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class CompleteRequestPanel extends JPanel implements ChatIF {
	private AcademicFrame frame;
	private CompleteRequestPanel completeRequestPanel;
	private SupervisorPanel supervisorPanel;
	private JComboBox comboBox;
	private ViewRequestPanel viewRequest;
	
	
	
	public CompleteRequestPanel(AcademicFrame frame,SupervisorPanel supervisorPanel) {
		super();
		this.frame=frame;
		this.supervisorPanel=supervisorPanel;
		this.completeRequestPanel=this;
		frame.setSize(500, 400);
		initialize();
		
	}
	
	
private void initialize() {
		setLayout(null);
		
		JLabel lblCompleteRequest = new JLabel("Complete Request");
		lblCompleteRequest.setForeground(Color.BLUE);
		lblCompleteRequest.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblCompleteRequest.setBounds(111, 22, 256, 22);
		add(lblCompleteRequest);
		
		JLabel lblEligibleRequestTo = new JLabel("Eligible Request To complete");
		lblEligibleRequestTo.setBounds(43, 78, 174, 14);
		add(lblEligibleRequestTo);
		
		comboBox = new JComboBox();
		comboBox.setBounds(43, 112, 150, 20);
		add(comboBox);
		
		JButton btnComplete = new JButton("Complete");
		btnComplete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(comboBox.getSelectedItem().equals("--Choose a Request--"))
				{
				(new Dialog("Choose a request to Complete")).setVisible(true);
				}
							else
							{
						ArrayList<Object> completeReq =new ArrayList<Object>();
						completeReq.add("complete");
						completeReq.add(comboBox.getSelectedItem());
						completeReq.add("998");
						AcademicFrame.myclient.sendMessageToServer(completeReq);
							}
			}
				
			
		});
		btnComplete.setBounds(266, 111, 89, 23);
		add(btnComplete);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setSize(500, 500);
				frame.setContentPane(supervisorPanel);
			}
		});
		btnCancel.setBounds(171, 233, 89, 23);
		add(btnCancel);
		
		JButton btnDetails = new JButton("Details");
		btnDetails.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(comboBox.getSelectedItem().equals("--Choose a Request--"))
				{
					(new Dialog("Please choose a request from the list first.")).setVisible(true);
				}
				else{
					viewRequest = new ViewRequestPanel(comboBox.getSelectedItem().toString(),frame,completeRequestPanel,false,"");
					frame.setSize(550, 780);
					frame.setContentPane(viewRequest);
				}
			}
		});
		btnDetails.setBounds(67, 143, 89, 23);
		add(btnDetails);
	}

	@Override
	public void display(Object message) {
		// TODO Auto-generated method stub
		ArrayList<Object> answer=new ArrayList<Object>();
		answer.clear(); // clear the info array list
		answer = (ArrayList<Object>)message;
		
		if(answer.get(0).equals("completeReq"))
		{
			if(answer.get(2).equals("0"))
			{
				(new Dialog("No Available Requests")).setVisible(true);
				frame.setSize(500, 500);
				frame.setContentPane(supervisorPanel);
	
			}
			else{
			int numberOfreq = Integer.parseInt((answer.get(1)).toString()); 
			this.comboBox.addItem("--Choose a Request--");
			for (int i=0; i < numberOfreq ; i++ )
			{
				this.comboBox.addItem((String)answer.get(i+2));  
			}
			
			}
		}
		else
			if(answer.get(0).equals("changeStatus"))
			{
				if(answer.get(0).equals("statusNotSet"))
				{
					(new Dialog("Failed To Complete")).setVisible(true);
				}
				else
				{
					(new Dialog("Complete Request")).setVisible(true);
					
					frame.setContentPane(new CompleteRequestPanel(frame, supervisorPanel));
					frame.setSize(460, 420);
					
					ArrayList<Object> completeReq= new ArrayList<Object>();
					completeReq.add("completeReq");
					completeReq.add("62");
					completeReq.add("121");
					AcademicFrame.myclient.sendMessageToServer(completeReq); // get requests
				}
				
			}
	}

}
